import beans.BlockBean;
import beans.BlockchainBean;
import beans.Student;
import block.Block;
import block.Blockchain;
import org.junit.Test;
import utils.ByteArrToHexString;
import utils.PrintAllBlock;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * @author:zhengyilong
 * @email:1797344574@qq.com
 * @phone:13479394730
 * @desc:
 * @DateTime:2022/5/14 8:51
 **/
public class Main{
    @Test
    public void testPrintBlock(){
        BlockBean newBlock = Block.NewBlock("张三", null);

        byte[] data = newBlock.getData();
        long timestamp = newBlock.getTimestamp();
        byte[] prevBlockHash = newBlock.getPrevBlockHash();
        byte[] hash = newBlock.getHash();
        long nonce = newBlock.getNonce();

        System.out.println(new String(data,StandardCharsets.UTF_8));
        System.out.println(timestamp);
        System.out.println(ByteArrToHexString.getByteArrToHexString(prevBlockHash));
        System.out.println(ByteArrToHexString.getByteArrToHexString(hash));
        System.out.println(nonce);
    }

    @Test
    public void testPrintBlockchain(){
        Blockchain.NewBlockchain("创世区块");
        Blockchain.AddBlock("第二个区块");

        PrintAllBlock.printAllBlock();
    }

    @Test
    public void testOutput(){
        String path = "C:/Users/86134/Desktop/test.txt";

        ObjectOutputStream oos = null;
        try {
            oos= new ObjectOutputStream(new FileOutputStream(path));

            Student s1 = new Student("张三", 12);
            Student s2 = new Student("李四", 33);
            oos.writeObject(s1);
            oos.writeObject(s2);
            oos.writeObject(null);
            oos.flush();
            System.out.println("写入完毕");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testOutputToo(){
        String path = "C:/Users/86134/Desktop/test.txt";

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            Object object = ois.readObject();
            ArrayList<Object> objects = new ArrayList<>();
            while (object != null){
                //System.out.println(object);
                objects.add(object);
                object = ois.readObject();
            }

            oos= new ObjectOutputStream(new FileOutputStream(path));
            for (Object o : objects) {
                oos.writeObject(o);
            }
            Student s1 = new Student("王五", 12);
            Student s2 = new Student("赵六", 33);
            oos.writeObject(s1);
            oos.writeObject(s2);
            oos.writeObject(null);
            oos.flush();
            System.out.println("写入完毕");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testInput(){
        String path = "C:/Users/86134/Desktop/test.txt";

        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            Object object = ois.readObject();
            while (object != null){
                System.out.println(object);
                object = ois.readObject();
            }
        }catch (Exception e){
//            System.out.println("end");
            e.printStackTrace();
        }finally {
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
//        Blockchain.AddBlock("第三个区块");
        PrintAllBlock.printAllBlock();
        System.out.println("测试");
    }
}

